package com.isoftstone.util;

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        // 实例化串口操作类对象
        SerialPortUtils serialPort = new SerialPortUtils();
        // 创建串口必要参数接收类并赋值，赋值串口号，波特率，校验位，数据位，停止位
        ParamConfig paramConfig = new ParamConfig("COM1", 9600, 0, 8, 1);
        // 初始化设置,打开串口，开始监听读取串口数据
        serialPort.init(paramConfig);
        // 调用串口操作类的sendComm方法发送数据到串口
        serialPort.sendComm("01030016001625C0");

        //01030016001625C0
        //01032C3D88F1F5000000000000000041200000000000004100000000000047C3500041B4D5E23B03126F3F8000004C17
        Thread.sleep(5000);
        serialPort.closeSerialPort();
//        while (serialPort.isGuanbi()){
//
//
//        }
       // process();
    }

    public static void process() {
        try {
            Enumeration portList = CommPortIdentifier.getPortIdentifiers();
            CommPortIdentifier portId = null;
            while (portList.hasMoreElements())
            {
                portId = (CommPortIdentifier) portList.nextElement();
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)//如果端口类型是串口则判断名称
                {
                    if(portId.getName().equals("COM5")){//如果是COM1端口则退出循环
                        break;
                    }else{
                        portId=null;
                    }
                }
            }
            SerialPort serialPort = (SerialPort)portId.open(Object.class.getSimpleName(), 2000);//打开串口的bai超时时间为1000ms
            serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//设置串口速率为9600，数据位8位，停止位1们，奇偶校验无
            InputStream in = serialPort.getInputStream();//得到输入流
            OutputStream outputStream = serialPort.getOutputStream();//得到输出流
            byte[] writerBuffer = null;
            try {
                writerBuffer = SerialPortUtils.hexToByteArray("01030016000225CF");
            } catch (NumberFormatException e) {
                throw new RuntimeException("命令格式错误！");
            }
            try {
                outputStream.write(writerBuffer);
                outputStream.flush();
            } catch (NullPointerException e) {
                throw new RuntimeException("找不到串口。");
            } catch (IOException e) {
                throw new RuntimeException("发送信息到串口时发生IO异常");
            }
            // 通过输入流对象的available方法获取数组字节长度
            byte[] readBuffer = new byte[in.available()];
            int len;
            while ((len = in.read(readBuffer)) != -1) {
                // 直接获取到的数据
                // 转为十六进制数据
                String dataHex = SerialPortUtils.bytesToHexString(readBuffer);
                System.out.println("dataHex:" + dataHex);// 读取后置空流对象
            }

//进行输入输出操作
//操作结束后
            in.close();
            outputStream.close();
            serialPort.close();//关闭串口
        } catch (PortInUseException e) {
            e.printStackTrace();
        } catch (UnsupportedCommOperationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
